package cn.itcast.exercise

import java.io.{File, PrintWriter}

import scala.collection.mutable
import scala.io.Source

/**
  * @Package cn.itcast.exercise
  * @author KTL
  * @date 2021/5/21 0021 8:00
  * @version V1.0
  * @Copyright © 2015-04-29  One for each, and two for each
  *           DESC 从文件价加载数据读取
  */
object WoudCount4 {
  def main(args: Array[String]): Unit = {
    val dir = new File("data/input")
    val files: Array[File] = dir.listFiles()
    println(files.toBuffer)

    //遍历文件夹下的每一个文件，并做wordcount，最后在汇总每一个
    val eachFileResult: Array[Map[String, Int]] = files.map(file => {
      val lines: List[String] = Source.fromFile(file).getLines().toList
      val oneFileResult: Map[String, Int] = lines.flatMap(_.split(" "))
        .map(t => (t, 1))
        .groupBy(t => t._1)
        .mapValues(t => t.length)
      oneFileResult
    })
    println(eachFileResult.toBuffer)


    val tempResult: Array[(String, Int)] = eachFileResult.flatten
    println(tempResult.toBuffer)

    val groubyResult: Map[String, Array[(String, Int)]] = tempResult.groupBy(t=>t._1)
    val tempMap: Map[String, mutable.Buffer[(String, Int)]] = groubyResult.mapValues(_.toBuffer)
    println(tempMap)

    //聚合

    val result: Map[String, Int] = groubyResult.mapValues(_.map(_._2).sum)
    println(result)

    val writer = new PrintWriter(new File("data/output/result.txt"))
    result.foreach(writer.println)
    writer.close()
  }
}
